package dr.incomeapp.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import dr.incomeapp.Application;
import dr.incomeapp.R;
import dr.incomeapp.bean.CategoryBean;
import dr.incomeapp.bean.IncomeBean;
import dr.incomeapp.bean.IncomeBean.TIMEEVENT;
import dr.incomeapp.model.FactoryModel;
import dr.incomeapp.model.IncomeEditModel;

public class IncomeEditController extends MyController implements OnItemSelectedListener{

	protected EditText etName;
	protected EditText etDescription;
	protected EditText etCost;
	protected EditText etMoneyProg;
	protected Button btnStartDate;
	protected Button btnEndDate;
	protected Button btnSave;
	protected Button btnDelete;
	
	
	protected RadioButton rbtnCost;
	protected RadioButton rbtnIncome;
	protected RadioGroup rgrpType;
	
	protected Spinner spTimeEvent;
	protected Spinner spCategory;
	
	DatePicker dpStartDate;
	DatePicker dpEndDate;
	
	

	protected IncomeEditModel model = FactoryModel.getIncomeEditModel();
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_income_edit);

		etName = (EditText) findViewById(R.id.etName);
		etCost = (EditText) findViewById(R.id.etCost);
		etMoneyProg = (EditText) findViewById(R.id.etMoneyProg);
		btnStartDate = (Button) findViewById(R.id.btnStartDate);
		btnEndDate = (Button) findViewById(R.id.btnEndDate);
		etDescription = (EditText) findViewById(R.id.etDescription);
		
		spTimeEvent = (Spinner) findViewById(R.id.spTimeEvent);
		spCategory = (Spinner) findViewById(R.id.spCategory);
		
		rbtnCost = (RadioButton) findViewById(R.id.rbtnCost);
		rbtnIncome = (RadioButton) findViewById(R.id.rbtnIncome);
		rgrpType = (RadioGroup) findViewById(R.id.rgrpType);
		
		//rgrpType.setOnClickListener(this);
		

		btnSave = (Button) findViewById(R.id.btnSave);
		btnDelete = (Button) findViewById(R.id.btnDelete);

		btnSave.setOnClickListener(this);
		btnDelete.setOnClickListener(this);
		btnStartDate.setOnClickListener(this);
		btnEndDate.setOnClickListener(this);
		
		
		
        Intent intent = getIntent();
        IncomeBean incomeBean = (IncomeBean)intent.getSerializableExtra(IncomeBean.BEANNAME);

        clear();
        
        if(incomeBean != null){


        }else{
        	incomeBean = new IncomeBean();
        }
    	model.setIncomeBean(incomeBean);
    	
    	initCategory();

    	
        init();

	}
	
	
	public void initCategory(){
		List<String> listCategory = new LinkedList<String>();
		model.readCategory();
    	for(CategoryBean cB: model.getListCategory()){
    		String value = cB.getName();
    		listCategory.add(value);
    	}
    	listCategory.add("new Category");
    	
    	  	
    	ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,listCategory);

    	spCategory.setAdapter(spinnerArrayAdapter);
    	spCategory.setOnItemSelectedListener(this);
	}
	

	public void init(){
		etName.setText(model.getIncomeBean().getName());
		btnStartDate.setText(model.getIncomeBean().getStartDateFormated());
		btnEndDate.setText(model.getIncomeBean().getEndDateFormated());
		etCost.setText(String.valueOf(model.getIncomeBean().getMoney()));
		etMoneyProg.setText(String.valueOf(model.getIncomeBean().getMoneyProg()));
		

		rbtnCost.setChecked(model.getIncomeBean().isCost());
		rbtnIncome.setChecked(!model.getIncomeBean().isCost());
		
		

		
		if(model.isNew()){
			btnDelete.setVisibility(View.GONE);
			spCategory.setSelection(1);
		}else{
			btnDelete.setVisibility(View.VISIBLE);
			spCategory.setSelection(model.getListCategory().indexOf(model.getIncomeBean().getCategoryBean()));
			
		}
	}
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_income, menu);
		return true;
	}


	
	public void onClick(View v) {
		if(btnSave.equals(v)){
			model.getIncomeBean().setName(etName.getText().toString());
			model.getIncomeBean().setEnTimeEvent(TIMEEVENT.values()[spTimeEvent.getSelectedItemPosition()]);
			
			model.getIncomeBean().setStartDate(Util.stringToDate(btnStartDate.getText().toString()));
			model.getIncomeBean().setEndDate(Util.stringToDate(btnEndDate.getText().toString()));
			
			model.getIncomeBean().setDescription(etDescription.getText().toString());
			
			
		    model.getIncomeBean().setMoney(Float.parseFloat(etCost.getText().toString()));
		    model.getIncomeBean().setMoneyProg(Float.parseFloat(etMoneyProg.getText().toString()));
		    
		    if(rgrpType.getCheckedRadioButtonId() == rbtnCost.getId()){
		    	model.getIncomeBean().setCost(true);
		    }else{
		    	model.getIncomeBean().setCost(false);
		    }
		    
		    
		    CategoryBean selectedCategory = model.getListCategory().get(spCategory.getSelectedItemPosition()); 
		    model.getIncomeBean().setCategoryBean(selectedCategory);
		    
			model.createOrUpdate();
			finish();
			
		}else if(btnDelete.equals(v)){
			model.delete();
			finish();
			
		}
		else if(btnStartDate.equals(v)){
			
			dpStartDate = Util.createDatePickerDialog(this,model.getIncomeBean().getStartDate());

		}
		else if(btnEndDate.equals(v)){
			dpEndDate = Util.createDatePickerDialog(this,model.getIncomeBean().getEndDate());
		}

	}
	
	public void onDateSet(DatePicker dp, int year, int monthOfYear,
			int dayOfMonth) {

	
		Calendar ca2 = Calendar.getInstance();
		ca2.set(year, monthOfYear, dayOfMonth);
		
		Date da = ca2.getTime();
		
		if(dp.equals(dpStartDate)){
			btnStartDate.setText(Util.getDateFormated(da));
		}else if(dp.equals(dpEndDate)){
			btnEndDate.setText(Util.getDateFormated(da));
		}

	}
	
	

	
	protected Builder showCategoryDialog(){
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		alert.setTitle("Category");
		alert.setMessage("New Category...");
		

		// Set an EditText view to get user input
		final EditText input = new EditText(this);
		alert.setView(input);

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			
			
		  String value = input.getText().toString();
		  
		  CategoryBean cB = new CategoryBean();
		  cB.setCost(rgrpType.getCheckedRadioButtonId() == rbtnCost.getId());
		  cB.setName(value);
		  
		  model.insertNewCategory(cB);
		  
		  initCategory();
		  spCategory.setSelection(model.getListCategory().size()-1);
		  
		  // Do something with value!
		  }
		});

		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
		  public void onClick(DialogInterface dialog, int whichButton) {
		    spCategory.setSelection(1);
		  }
		});

		alert.show();
		return alert;
	}

	public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
			long arg3) {
		
		if(model.getListCategory().size() == pos){
			showCategoryDialog();
		}
		// TODO Auto-generated method stub
		
	}

	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
}
